<?php
/**
 * Framework_Util_CharacterEncoder
 *
 * 
 *
 * @package BAMF
*/

class Framework_Util_CharacterEncoder {


/**
 * encode
 *
 * Converts most special characters to their HTML equivalents.<br /><pre>NOTE: encode() may not work properly if the PHP ini setting magic_quotes_gpc is turned On.</pre>
 *
 * @param		string $aString  The string to encode
 * @param		int $quoteStyle  The quote_style (per htmlentities()). Default ENT_QUOTES.
 * @param		string $charSet  The character set to use. Default ISO-8859-1.
 * @param		boolean $stripSlashes  Whether to pass $aString through stripslashes(). Useful when magic_quotes_gpc is On. Default true.
 * @param		boolean $stripTags  Whether to pass $aString through strip_tags(). Default false.
 *
 * @return		Encoded string 
 *
 * @package BAMF
 * 
 */

	public static function encode( $aString, $quoteStyle=ENT_QUOTES, $charSet="ISO-8859-1", $stripSlashes=true, $stripTags=false ) {

		$quoteStyle = empty( $quoteStyle ) ? ENT_QUOTES : $quoteStyle ;
		$charSet = empty( $charSet ) ? "ISO-8859-1" : $charSet ;

		$aString = str_replace( chr(147 /*8220*/), '"', $aString);
		$aString = str_replace( chr(148 /*8221*/), '"', $aString);  //"\"" or "&quot;"
		$aString = str_replace( chr(145 /*8216*/), "'", $aString);
		$aString = str_replace( chr(146 /*8217*/), "'", $aString);  //"'" or "&#039;"

		if( $stripTags === true ) {
			$aString = strip_tags( $aString ) ;
		}

		if( $stripSlashes === true ) {
			$aString = stripslashes( $aString ) ;
		}

		return htmlentities( trim( $aString ), $quoteStyle, $charSet ) ;
	}


	public static function decode( $aString, $quoteStyle=ENT_QUOTES, $charSet="ISO-8859-1" ) {
		return html_entity_decode( trim( $aString ), $quoteStyle, $charSet ) ;
	}


}


?>
